package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.LeaseAgreement;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.admin.mapper.LeaseAgreementMapper;
import com.atguigu.lease.web.admin.service.ApartmentInfoService;
import com.atguigu.lease.web.admin.service.LeaseAgreementService;
import com.atguigu.lease.web.admin.vo.agreement.AgreementQueryVo;
import com.atguigu.lease.web.admin.vo.agreement.AgreementVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.rmi.dgc.Lease;

/**
 * @author liubo
 * @description 针对表【lease_agreement(租约信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class LeaseAgreementServiceImpl extends ServiceImpl<LeaseAgreementMapper, LeaseAgreement>
        implements LeaseAgreementService {
   @Autowired
   private LeaseAgreementMapper leaseAgreementMapper;
    /**
     * 根据条件分页查询租约列表
     * 设计表：公寓、房间、支付方式、租期
     * @param page
     * @param queryVo
     */
    @Override
    public void customPage(Page<AgreementVo> page, AgreementQueryVo queryVo) {
        leaseAgreementMapper.customSelectList(page,queryVo);
    }

    /**
     * 根据id查询租约详情信息
     * @param id
     * @return
     */
    @Override
    public AgreementVo customGetById(Long id) {
        return leaseAgreementMapper.customSelectById(id);
    }

    /**
     * 根据id删除租约信息，
     * 如果在租约状态，则不能删除,抛出异常
     * @param id
     */
    @Override
    public void customRemoveById(Long id) {
        //查询租约
        LambdaQueryWrapper<LeaseAgreement> leaseAgreementLambdaQueryWrapper=new LambdaQueryWrapper<>();
        leaseAgreementLambdaQueryWrapper.eq(LeaseAgreement::getId,id);
        leaseAgreementLambdaQueryWrapper.in(LeaseAgreement::getStatus,
                LeaseStatus.SIGNED.getCode(),
                LeaseStatus.WITHDRAWING.getCode(),
                LeaseStatus.RENEWING.getCode());
        LeaseAgreement leaseAgreement = getOne(leaseAgreementLambdaQueryWrapper);
        //如果在租状态，则不能删除,抛出异常
        if(leaseAgreement != null){
            throw new LeaseException(ResultCodeEnum.ILLEGAL_REQUEST);
        }
        //否则，删除租约
        removeById(id);
    }
}




